const net = require('net')

console.log('mock执行');
const server = net.createServer(socket => {
    let i = 0;
    const handler = setInterval(() => {
        i++;
        const time = i * 20;
        const data = (100 * (Math.sin(2 * 3.14 / 6000 * time))).toFixed(0);
        const paddingStartData = data.padStart(4, '0');
        if (socket) {
            const buffer = new ArrayBuffer(4);
            const view = new Uint8Array(buffer)
            view[2] = 1
            view[3] = (100 * (Math.sin(2 * 3.14 / 6000 * time))).toFixed(0);
            socket.write(view);
        }
    }, 20);
    socket.on('connect', () => {
        console.log('client connect to mock socket server')
        handler()
    })
    socket.on('close', () => {
        clearInterval(handler)
        console.log('mock socket server closed')
    })
    socket.on('error', () => {
        console.log('mock socket server error')
    })
}).listen(8088)

server.on('listening', function () {
    console.log('listening')
})
